<?php

register_shutdown_function(function() {
  $lastError = error_get_last();
  if (!empty($lastError)) {
    header("HTTP/1.1 500 Internal Server Error", true, 500);
  }
});

include '../config.inc.php';
include _FLEP_LIB_DIR_.'save.inc.php';

flep_session_start();

function felp_delete_structure($data) {
  if (!flep_structure($structure, $data['type'])) {
    $_SESSION['error'] = "Error: Can't find type: " . $data['type'];
  }

  $structure['data'][0]['id'] = $data['id'];

  if($data['type'] === 'user' && intval(flep_get($_SESSION['user'], 'id')) === intval(flep_decode($structure['data'][0]['id']))){
    $_SESSION['error'] = _FLEP_CURRENT_USER_DELETE_MSG_;
  } else {
    if (flep_delete($structure)) {
      $_SESSION['msg'] = 'Eliminado Exitosamente.';
    } else {
      $_SESSION['error'] = (array_key_exists($structure['errno'], $GLOBALS['DB_ERRORS'])? sprintf($GLOBALS['DB_ERRORS'][$structure['errno']],$data['type']):$structure['msg']);
    }
  }
  header('Location: '.$_SESSION['on_delete'][$data['type']]);
  exit();
}

if (isset($_GET["id"]) && isset($_GET["type"])) {
  if (!flep_valid_login($_SESSION['user'])) {
    header("Location: index.php");
    exit();
  }
  felp_delete_structure($_GET);
  exit();
}

header("Location: index.php");
?>
